package com.lonely.fx;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.DateCell;
import javafx.scene.control.DatePicker;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.StringConverter;

import java.time.LocalDate;
import java.time.chrono.Chronology;
import java.util.Locale;

/**
 * @date: 2022-07-18 15:45
 * @author: ztkj-hzb
 * @description:
 **/
public class DatePickerSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {


        HBox hBox = new HBox();

        //日期选择器
        //DatePicker datePicker = new DatePicker();
        //初始化带时间得
        DatePicker datePicker = new DatePicker(LocalDate.now());

        //弹出窗口是否应显示显示周数的列 默认为false
        //datePicker.setShowWeekNumbers(true);

        //设置时间框禁止编辑
        datePicker.setEditable(false);

        //设置选中后显示在文本框上的内容格式
        datePicker.setConverter(new StringConverter<LocalDate>() {
            @Override
            public String toString(LocalDate object) {

                if(object == null){
                    return null;
                }

                int year = object.getYear();
                int monthValue = object.getMonthValue();
                int dayOfMonth = object.getDayOfMonth();
                int value = object.getDayOfWeek().getValue();

                return String.format("%s/%s/%s 星期%s",year,monthValue,dayOfMonth,value);
            }

            @Override
            public LocalDate fromString(String string) {
                return null;
            }
        });


        //定制化弹窗的样式
        datePicker.setDayCellFactory(new Callback<DatePicker, DateCell>() {
            @Override
            public DateCell call(DatePicker param) {

                //每个item添加一个背景颜色
                DateCell dateCell = new DateCell(){

                    /**
                     * {@inheritDoc}
                     *
                     * @param item
                     * @param empty
                     */
                    @Override
                    public void updateItem(LocalDate item, boolean empty) {
                        super.updateItem(item, empty);

                        this.setStyle("-fx-background-color: pink");

                    }
                };

                return dateCell;
            }
        });


        hBox.getChildren().addAll(datePicker);

        primaryStage.setScene(new Scene(hBox));
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);
        primaryStage.show();
    }
}
